package com.example.demo02.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo02.common.Result;
import com.example.demo02.dto.OrderDetailWithProductInfo;
import com.example.demo02.entity.OrderDetails;
import com.example.demo02.entity.Product;
import com.example.demo02.service.OrderDetailsService;
import com.example.demo02.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/order-detail")
@CrossOrigin
public class OrderDetailsController {

    @Autowired
    OrderDetailsService orderDetailsService;

    @Autowired
    ProductService productService;

    @PostMapping("/add")
    public Result add(@RequestBody OrderDetails orderDetails) {
        try {
            orderDetailsService.save(orderDetails);
        } catch (Exception e) {
            if (e instanceof DuplicateKeyException) {
                return Result.error("插入数据库错误");
            } else {
                return Result.error("系统错误");
            }
        }
        return Result.success();
    }

    @PutMapping("/update")
    public Result update(@RequestBody OrderDetails orderDetails) {
        orderDetailsService.updateById(orderDetails);
        return Result.success();
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        orderDetailsService.removeById(id);
        return Result.success();
    }

    @DeleteMapping("/delete/batch")
    public Result batchDelete(@RequestBody List<Integer> ids) {
        orderDetailsService.removeByIds(ids);
        return Result.success();
    }

    @GetMapping("/selectAll")
    public Result selectAll() {
        List<OrderDetails> orderDetailsList = orderDetailsService.list();
        return Result.success(orderDetailsList);
    }

    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id) {
        OrderDetails orderDetails = orderDetailsService.getById(id);
        return Result.success(orderDetails);
    }

    //根据order_id查询订单详情并根据productId读取商品信息

    @GetMapping("/selectByOrderId")
    public Result selectByOrderId(@RequestParam Integer orderId) {
        // 使用MyBatis-Plus的lambda表达式来查询数据
        List<OrderDetailWithProductInfo> orderDetailsWithProductInfoList = orderDetailsService.lambdaQuery()
                .eq(OrderDetails::getOrderId, orderId)
                .list()
                .stream()
                .map(orderDetail -> {
                    Product product = productService.getById(orderDetail.getProductId());
                    return new OrderDetailWithProductInfo(orderDetail, product);
                })
                .collect(Collectors.toList());

        return Result.success(orderDetailsWithProductInfoList);
    }

    @GetMapping("/selectByPage")
    public Result selectByPage(@RequestParam Integer pageNum,
                               @RequestParam Integer pageSize,
                               @RequestParam(required = false) Integer orderId) {
        Page<OrderDetails> page = new Page<>(pageNum, pageSize);
        Page<OrderDetails> orderDetailsPage = orderDetailsService.page(page, new QueryWrapper<OrderDetails>()
                .eq(orderId != null, "order_id", orderId));
        return Result.success(orderDetailsPage);
    }
}